package org.softmed.rest.editor.comps.browser;

import java.util.ArrayList;
import java.util.List;

public class PagedList {
	long currentPageNumber;
	long itemsPerPage;
	long itemCount;

	String previousURI;
	String nextURI;
	String firstURI;
	String lastURI;

	List items = new ArrayList();

	public void setupPage(boolean detailed, long count, int currentPage,
			String baseURI) {

		previousURI = null;
		nextURI = null;
		firstURI = null;
		lastURI = null;

		currentPageNumber = currentPage;

		itemCount = count;

		int lastPage = (int) (count / (int) itemsPerPage);
		if (count % itemsPerPage != 0)
			lastPage += 1;

		long initialIndex = itemsPerPage * (currentPageNumber - 1);
		long finalIndex = itemsPerPage * currentPageNumber - 1;

		if (count > itemsPerPage) {

			if (count > finalIndex + 1) {
				nextURI = baseURI + getCorrectSizeParam()
						+ isDetailed(detailed) + "&page="
						+ (currentPageNumber + 1);
			}

			if (initialIndex > 0) {
				previousURI = baseURI + getCorrectSizeParam()
						+ isDetailed(detailed) + "&page="
						+ (currentPageNumber - 1);
				firstURI = baseURI + getCorrectSizeParam()
						+ isDetailed(detailed) + "&page=1";
			}

			if (currentPageNumber != lastPage)
				lastURI = baseURI + getCorrectSizeParam()
						+ isDetailed(detailed) + "&page=" + lastPage;
		}

	}

	protected String isDetailed(boolean detailed) {
		return (detailed ? "&detailed" : "");
	}

	private String getCorrectSizeParam() {
		return "?size=" + itemsPerPage;
	}

	public long getCurrentPageNumber() {
		return currentPageNumber;
	}

	public void setCurrentPageNumber(int currentPageNumber) {
		this.currentPageNumber = currentPageNumber;
	}

	public long getItemsPerPage() {
		return itemsPerPage;
	}

	public void setItemsPerPage(int itemsPerPage) {
		this.itemsPerPage = itemsPerPage;
	}

	public long getItemCount() {
		return itemCount;
	}

	public void setItemCount(long itemCount) {
		this.itemCount = itemCount;
	}

	public String getPreviousURI() {
		return previousURI;
	}

	public void setPreviousURI(String previousURI) {
		this.previousURI = previousURI;
	}

	public String getNextURI() {
		return nextURI;
	}

	public void setNextURI(String nextURI) {
		this.nextURI = nextURI;
	}

	public String getFirstURI() {
		return firstURI;
	}

	public void setFirstURI(String firstURI) {
		this.firstURI = firstURI;
	}

	public String getLastURI() {
		return lastURI;
	}

	public void setLastURI(String lastURI) {
		this.lastURI = lastURI;
	}

	public List getItems() {
		return items;
	}

	public void setItems(List list) {
		this.items = list;
	}

}
